/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.ChiTietDauGiaDAO;
import dao.DanhMucDAO;
import dao.DanhMucNhaSanXuatDAO;
import dao.HinhAnhDAO;
import dao.LoaiDauGiaDAO;
import dao.NhaSanXuatDAO;
import dao.SanphamDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Chitietdaugia;
import model.Danhmuc;
import model.DanhmucNhasanxuat;
import model.Hinhanh;
import model.Loaidaugia;
import model.Nhasanxuat;
import model.Sanpham;

/**
 *
 * @author HoangLiem
 */
@WebServlet(name = "capNhatSanPham", urlPatterns = {"/capNhatSanPham"})
public class capNhatSanPham extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            //Chuan bị dữ liệu view
            ArrayList<Danhmuc> dsDanhMuc = DanhMucDAO.getAll();
            ArrayList<Nhasanxuat> dsNhaSanXuat = NhaSanXuatDAO.getAll();
            ArrayList<Loaidaugia> dsLoaiDauGia = LoaiDauGiaDAO.getAllBidTypes();
            ArrayList<Integer> dsGio = new ArrayList<Integer>();
            ArrayList<Integer> dsPhut = new ArrayList<Integer>();

            //Định nghĩa các ngày tháng năm
            ArrayList<Integer> dsThang = new ArrayList<Integer>();
            ArrayList<Integer> dsNgay = new ArrayList<Integer>();
            ArrayList<Integer> dsNam = new ArrayList<Integer>();
            for (int i = 1; i <= 31; i++) {
                dsNgay.add(i);
                if (i < 13) {
                    dsThang.add(i);
                }
            }
            for (int i = 2011; i <= 2020; i++) {
                dsNam.add(i);
            }
            for (int i = 0; i < 24; i++) {
                dsGio.add(i);
            }
            for (int i = 0; i < 60; i++) {
                dsPhut.add(i);
            }
            ///////////
            String maSanPham = "";
            if (request.getParameter("masp") != null) {
                maSanPham = request.getParameter("masp");
            }
            session.setAttribute("TrangHienTai", "capNhatSanPham?masp=" + maSanPham);
            Sanpham sanPham = SanphamDAO.getProduct(maSanPham);
            
            request.setAttribute("sanPham", sanPham);
            request.setAttribute("dsNgay", dsNgay);
            request.setAttribute("dsThang", dsThang);
            request.setAttribute("dsNam", dsNam);
            request.setAttribute("dsGio", dsGio);
            request.setAttribute("dsPhut", dsPhut);
            request.setAttribute("dsDanhMuc", dsDanhMuc);
            request.setAttribute("dsNhaSanXuat", dsNhaSanXuat);
            request.setAttribute("dsLoaiDauGia", dsLoaiDauGia);

            //Lay du lieu tu form cap nhat lai csdl
            Sanpham sp = new Sanpham();
            if (request.getParameter("maSanPham") != null) {
                sp.setMaSanPham(request.getParameter("maSanPham"));
            }
            if (request.getParameter("tenSanPham") != null) {
                sp.setTenSanPham(request.getParameter("tenSanPham"));
            }
            if (request.getParameter("moTa") != null) {
                sp.setMoTa(request.getParameter("moTa"));
            }
            if (request.getParameter("giaKhoiDiem") != null) {
                sp.setGiaKhoiDiem(Float.parseFloat(request.getParameter("giaKhoiDiem")));
            }
            if (request.getParameter("giaBanLien") != null) {
                sp.setGiaBanLien(Float.parseFloat(request.getParameter("giaBanLien")));
            }
            //Lay ngay bat dau
            int ngay = 1, thang = 1, nam = 2011, gio = 0, phut = 0;
            if (request.getParameter("thangNBD") != null) {
                thang = Integer.parseInt((String) request.getParameter("thangNBD"));
            }
            if (request.getParameter("ngayNBD") != null) {
                ngay = Integer.parseInt((String) request.getParameter("ngayNBD"));
            }
            if (request.getParameter("namNBD") != null) {
                nam = Integer.parseInt((String) request.getParameter("namNBD"));
            }
            if (request.getParameter("gioNBD") != null) {
                gio = Integer.parseInt((String) request.getParameter("gioNBD"));
            }
            if (request.getParameter("phutNBD") != null) {
                phut = Integer.parseInt((String) request.getParameter("phutNBD"));
            }
            
            Calendar cal = Calendar.getInstance();
            cal.set(nam, thang - 1, ngay, gio, phut);
            sp.setThoiDiemBatDau(cal.getTime());
            
            if (request.getParameter("thangNKT") != null) {
                thang = Integer.parseInt((String) request.getParameter("thangNKT"));
            }
            if (request.getParameter("ngayNKT") != null) {
                ngay = Integer.parseInt((String) request.getParameter("ngayNKT"));
            }
            if (request.getParameter("namNKT") != null) {
                nam = Integer.parseInt((String) request.getParameter("namNKT"));
            }
            if (request.getParameter("gioNKT") != null) {
                gio = Integer.parseInt((String) request.getParameter("gioNKT"));
            }
            if (request.getParameter("phutNKT") != null) {
                phut = Integer.parseInt((String) request.getParameter("phutNKT"));
            }
            
            cal.set(nam, thang - 1, ngay, gio, phut);
            sp.setThoiDiemKetThuc(cal.getTime());
            
            if (request.getParameter("tinhTrang") != null) {
                sp.setTinhTrang(Integer.parseInt(request.getParameter("tinhTrang")));
            }
            if (request.getParameter("trangThai") != null) {
                sp.setTrangThai(Boolean.parseBoolean(request.getParameter("trangThai")));
            }
            
            int maLoaiDauGia = 1;
            if (request.getParameter("maLoaiDauGia") != null) {
                maLoaiDauGia = Integer.parseInt(request.getParameter("maLoaiDauGia"));
                sp.setLoaidaugia(LoaiDauGiaDAO.getABidType(maLoaiDauGia));
            }
            String maDanhMuc = "";
            if (request.getParameter("maDanhMuc") != null) {
                maDanhMuc = (String) request.getParameter("maDanhMuc");
            }
            String maNhaSanXuat = "";
            if (request.getParameter("maNhaSanXuat") != null) {
                maNhaSanXuat = (String) request.getParameter("maNhaSanXuat");
            }
            if (request.getParameter("maSanPham") != null) {
                Danhmuc danhMuc = DanhMucDAO.getCategoryById(maDanhMuc);
                Nhasanxuat nhaSanXuat = NhaSanXuatDAO.getManufacturerById(maNhaSanXuat);
                DanhmucNhasanxuat dmNsx = DanhMucNhaSanXuatDAO.getACategory_Manufacturer(danhMuc, nhaSanXuat);
                sp.setDanhmucNhasanxuat(dmNsx);
            }

            //Xu ly luu update
            boolean kq = false;
            String url = "";
            if (request.getParameter("maSanPham") != null) {
                
                ArrayList<Hinhanh> dsHinh = HinhAnhDAO.getImage(sp.getMaSanPham());
                Set<Hinhanh> hinhanhs = new HashSet<Hinhanh>(0);
                hinhanhs.addAll(dsHinh);
                sp.setHinhanhs(hinhanhs);
                ArrayList<Chitietdaugia> dsCTDG = ChiTietDauGiaDAO.getBidDetails(sp.getMaSanPham());
                Set<Chitietdaugia> chitietdaugias = new HashSet<Chitietdaugia>(0);
                chitietdaugias.addAll(dsCTDG);
                sp.setChitietdaugias(chitietdaugias);
                
                kq = SanphamDAO.insertOrUpdate(sp);
                
                request.setAttribute("kq", kq);
                //url = (String) session.getAttribute("TrangHienTai");
                request.setAttribute("sanPham", sp);
                url = "capnhatsanphamPage.jsp";
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
                
            } else {
                url = "capnhatsanphamPage.jsp";
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            }
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
